from tortoise import fields, models

# 商品分类模型类
class ProductCategory(models.Model):
    """商品分类模型"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, unique=True, description="分类名称")
    parent = fields.ForeignKeyField("models.ProductCategory", related_name="children",null=True,on_delete=fields.SET_NULL,description="父级分类（NULL表示一级分类）")
    description = fields.TextField(description="分类描述", default="", null=True)
    created_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_time = fields.DatetimeField(auto_now=True, description="更新时间")
    is_active = fields.BooleanField(default=True, description="是否激活")

    class Meta:
        table = "product_category"

# 商品模型类
class Product(models.Model):
    """商品模型"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=100, index=True, description="商品名称")
    description = fields.TextField(description="商品描述", default="", null=True)
    category = fields.ForeignKeyField("models.ProductCategory",related_name="products",description="商品分类",on_delete=fields.CASCADE)    
    price = fields.DecimalField(max_digits=10, decimal_places=2, description="商品价格")
    image_url = fields.CharField(max_length=255, description="图片/视频URL")
    video_url = fields.CharField(max_length=255, description="类型（image/video）")
    inventory = fields.IntField(default=0, description="商品库存")
    sales = fields.IntField(default=0, description="商品销量")
    is_active = fields.BooleanField(default=True, description="是否上架")
    is_hot = fields.BooleanField(default=False, description="是否热销")
    share_count = fields.IntField(default=0, description="分享次数")
    created_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table = "product"
# 商品规格模型类
class ProductSpec(models.Model):
    """商品规格模型"""
    id = fields.IntField(pk=True)
    product = fields.ForeignKeyField("models.Product", related_name="specs", description="商品", on_delete=fields.CASCADE)
    name = fields.CharField(max_length=50, description="规格名称")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    is_enabled = fields.BooleanField(default=True, description="是否启用")

    class Meta:
        table = "product_spec"

# 商品规格选项表模型类
class ProductSpecOption(models.Model):
    """商品规格选项模型"""
    id = fields.IntField(pk=True)
    spec = fields.ForeignKeyField("models.ProductSpec", related_name="options", description="规格", on_delete=fields.CASCADE)
    name = fields.CharField(max_length=50, description="规格选项名称")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table = "product_spec_option"

# 商品小料表
class ProductTopping(models.Model):
    """商品小料表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50, description="小料名称")
    price = fields.DecimalField(max_digits=10, decimal_places=2, description="小料价格")
    product = fields.ForeignKeyField("models.Product", related_name="toppings", on_delete=fields.CASCADE)
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    is_enabled = fields.BooleanField(default=True, description="是否启用")

    class Meta:
        table = "product_topping"

# 商品评价表
class ProductComment(models.Model):
    """商品评价模型"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50, null=True, description="评论名称")
    product = fields.ForeignKeyField("models.Product", related_name="comments", description="商品", on_delete=fields.CASCADE)
    spec_option = fields.ForeignKeyField("models.ProductSpecOption", related_name="comments", null=True, on_delete=fields.CASCADE, description="购买的规格选项")
    user = fields.ForeignKeyField("models.User", related_name="comments", description="用户", on_delete=fields.CASCADE)
    order = fields.ForeignKeyField("models.OrderFake", related_name="comments", description="订单", on_delete=fields.CASCADE)
    parent = fields.ForeignKeyField("models.ProductComment", related_name="replies", null=True, blank=True, on_delete=fields.CASCADE,description="父评论ID")
    rating = fields.IntField(description="评分（1-5星）")
    content = fields.TextField(description="评价内容")
    images = fields.CharField(max_length=255,null=True, description="评价图片")
    created_at = fields.DatetimeField(auto_now_add=True, description="评价时间")
    reply_content = fields.TextField(description="回复内容", null=True, blank=True)
    is_auto = fields.BooleanField(default=False, description="是否自动评价")

    class Meta:
        table = "product_comment"


